package eitwitter;

import java.io.File;
import java.util.EnumMap;

import eitwitter.evaluation.SimpleEvaluation;
import eitwitter.learning.Learning;
import eitwitter.learning.models.BinomialModel;
import eitwitter.learning.models.ClassificationModel;
import eitwitter.learning.models.MultinomialModel;
import eitwitter.normalization.Normalizer;
import eitwitter.normalization.SizeTokenizer;
import eitwitter.normalization.SymbolsLinksTokenizer;
import eitwitter.storage.Category;
import eitwitter.storage.Company;
import eitwitter.storage.CostsMatrix;

/**
 * Programme principal - entrainement d'un classifieur par company
 * @author AH & PJ
 *
 */
public class MainLearningByCompany {
	
	/** Chemin vers le fichier d'entrainement */
	final public static String TRAINING_FILE_PATH = "fichiers/decoupe/train.txt";
	
	/** Chemin vers le fichier de test */
	public static String TEST_FILE_PATH = "fichiers/decoupe/test.txt";

	/** Chemin vers le fichier des mots vides */
	final public static String STOPWORDS_FILE_PATH = "fichiers/stoptweet.txt";

	/**
	 * Point d'entree de l'application
	 */
	public static void main(String[] args) {
		TEST_FILE_PATH = args[0];

		// Chargement du fichier d'apprentissage passÃ© en paramÃ¨tre
		File trainingFile = new File(TRAINING_FILE_PATH);
		
		// Chargement du fichier de test passÃ© en paramÃ¨tre
		File testFile = new File(TEST_FILE_PATH);

		// Chargement du fichier d'apprentissage passÃ© en paramÃ¨tre
		File stopwordsFile = new File(STOPWORDS_FILE_PATH);

		// Initialisation de la methode de normalisation a utiliser
		Normalizer normalizer = new SymbolsLinksTokenizer();

		// Initialisation du modele de classification
		ClassificationModel model = new BinomialModel(0.3);

		// Creation des classes d'apprentissage
		Learning learningGoogle = new Learning(normalizer, model, Company.GOOGLE);
		Learning learningApple = new Learning(normalizer, model, Company.APPLE);
		Learning learningTwitter = new Learning(normalizer, model, Company.TWITTER);
		Learning learningMicrosoft = new Learning(normalizer, model, Company.MICROSOFT);

		// Ajout de la liste des mots vides
		learningGoogle.addStopWordsFromFile(stopwordsFile);
		learningApple.addStopWordsFromFile(stopwordsFile);
		learningTwitter.addStopWordsFromFile(stopwordsFile);
		learningMicrosoft.addStopWordsFromFile(stopwordsFile);


		// Lancement de l'apprentissage avec le fichier d'entrainement
		learningGoogle.learnFromFile(trainingFile);
		learningApple.learnFromFile(trainingFile);
		learningTwitter.learnFromFile(trainingFile);
		learningMicrosoft.learnFromFile(trainingFile);

		SimpleEvaluation simpleEvaluationGoogle = new SimpleEvaluation(learningGoogle);
		SimpleEvaluation simpleEvaluationApple = new SimpleEvaluation(learningApple);
		SimpleEvaluation simpleEvaluationTwitter = new SimpleEvaluation(learningTwitter);
		SimpleEvaluation simpleEvaluationMicrosoft = new SimpleEvaluation(learningMicrosoft);
		
		
		simpleEvaluationApple.printTweetCategories(testFile);
		simpleEvaluationGoogle.printTweetCategories(testFile);
		simpleEvaluationTwitter.printTweetCategories(testFile);
		simpleEvaluationMicrosoft.printTweetCategories(testFile);
		
/*		EnumMap<Category, EnumMap<Category, Integer>> confusionMatrixGoogle = simpleEvaluationGoogle.computeConfusionMatrix(testFile);
		EnumMap<Category, EnumMap<Category, Integer>> confusionMatrixApple = simpleEvaluationApple.computeConfusionMatrix(testFile);
		EnumMap<Category, EnumMap<Category, Integer>> confusionMatrixTwitter = simpleEvaluationTwitter.computeConfusionMatrix(testFile);
		EnumMap<Category, EnumMap<Category, Integer>> confusionMatrixMicrosoft = simpleEvaluationMicrosoft.computeConfusionMatrix(testFile);
		
		for(Category category : Category.values()){
			System.out.println("Google");
			for(Category category2 : Category.values()){
				System.out.println(category+ " identifée comme "+category2+ " = "+ confusionMatrixGoogle.get(category).get(category2));
			}

		}
		
		for(Category category : Category.values()){
			System.out.println("Apple");
			for(Category category2 : Category.values()){
				System.out.println(category+ " identifée comme "+category2+ " = "+ confusionMatrixApple.get(category).get(category2));
			}

		}
		
		for(Category category : Category.values()){
			System.out.println("Twitter");
			for(Category category2 : Category.values()){
				System.out.println(category+ " identifée comme "+category2+ " = "+ confusionMatrixTwitter.get(category).get(category2));
			}

		}
		
		for(Category category : Category.values()){
			System.out.println("Microsoft");
			for(Category category2 : Category.values()){
				System.out.println(category+ " identifée comme "+category2+ " = "+ confusionMatrixMicrosoft.get(category).get(category2));
			}

		}

		int correctIndentifications = 0;
		int totalIdentifications = 0;
		int totalCost = 0;
		
		for(Category real : Category.values()){
			for(Category identified : Category.values()){
				totalCost += confusionMatrixTwitter.get(real).get(identified) * CostsMatrix.getInstance().getCost(real, identified);
				totalIdentifications += confusionMatrixTwitter.get(real).get(identified);
				if(real == identified){
					correctIndentifications += confusionMatrixTwitter.get(real).get(identified);
				}
				
			}
		}
		System.out.println("Twitter");
		System.out.println("Cout total = " + totalCost);
		System.out.println("Taux de reussite = " +100.*correctIndentifications / totalIdentifications);
		
		correctIndentifications = 0;
		totalIdentifications = 0;
		totalCost = 0;
		
		for(Category real : Category.values()){
			for(Category identified : Category.values()){
				totalCost += confusionMatrixGoogle.get(real).get(identified) * CostsMatrix.getInstance().getCost(real, identified);
				totalIdentifications += confusionMatrixGoogle.get(real).get(identified);
				if(real == identified){
					correctIndentifications += confusionMatrixGoogle.get(real).get(identified);
				}
				
			}
		}
		System.out.println("Google");
		System.out.println("Cout total = " + totalCost);
		System.out.println("Taux de reussite = " +100.*correctIndentifications / totalIdentifications);
		
		correctIndentifications = 0;
		totalIdentifications = 0;
		totalCost = 0;
		
		for(Category real : Category.values()){
			for(Category identified : Category.values()){
				totalCost += confusionMatrixApple.get(real).get(identified) * CostsMatrix.getInstance().getCost(real, identified);
				totalIdentifications += confusionMatrixApple.get(real).get(identified);
				if(real == identified){
					correctIndentifications += confusionMatrixApple.get(real).get(identified);
				}
				
			}
		}
		System.out.println("Apple");
		System.out.println("Cout total = " + totalCost);
		System.out.println("Taux de reussite = " +100.*correctIndentifications / totalIdentifications);
		
		correctIndentifications = 0;
		totalIdentifications = 0;
		totalCost = 0;
		
		for(Category real : Category.values()){
			for(Category identified : Category.values()){
				totalCost += confusionMatrixMicrosoft.get(real).get(identified) * CostsMatrix.getInstance().getCost(real, identified);
				totalIdentifications += confusionMatrixMicrosoft.get(real).get(identified);
				if(real == identified){
					correctIndentifications += confusionMatrixMicrosoft.get(real).get(identified);
				}
				
			}
		}
		System.out.println("Microsoft");
		System.out.println("Cout total = " + totalCost);
		System.out.println("Taux de reussite = " +100.*correctIndentifications / totalIdentifications);
		*/
	}
	

}
